﻿using System;
using System.Web.UI.WebControls;
using System.Linq;
using EPiServer.PlugIn;
using EPiServer.UI;
using EPiServer.Labs.MobilePack.Core.modules.MobilePack.Core;
using System.Web.UI;

namespace EPiServer.Labs.MobilePack.Core.Manifest
{
    /// <summary>
    /// Class for generating a Manifest file for offline storage
    /// </summary>
    [GuiPlugIn(DisplayName = "MobilePack Manifest Settings", Description = "Manifest Settings Page", Area = PlugInArea.AdminMenu, Url = "~/modules/MobilePack.Core/Manifest/AdminSettingsPage.aspx")]
    public partial class AdminSettingsPage : SystemPageBase
    {
          protected override void OnLoad(EventArgs e)
          {
              var model = ManifestFactory.Instance.LoadModel();
              if (model != null)
              {
                  IncludeStartPage.Checked = model.IncludeStartPage;
                  PageRoot.PageLink = model.StartPoint;
              }
              DataBind();          
          }

        /// <summary>
        /// Forcing update will save the new settings and also create a new manifest file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
          public void Force_Update(object sender, EventArgs e)
          {
              var model = ManifestFactory.Instance.LoadModel();
              if (model == null)
              {
                  model = new modules.MobilePack.Core.Manifest.ManifestModel();
              }
             
              model.StartPoint = PageRoot.PageLink;
              model.IncludeStartPage = IncludeStartPage.Checked;

              ManifestFactory.Instance.Save(model);

              ManifestFactory.Instance.FindPagesAndAddThem();
              ManifestFactory.Instance.WriteManifestFile();

              InfoMessage.Text = "File Generated";
              InfoMessage.Visible = true;
              InfoMessage.DataBind();

              FileListPanel.Controls.Clear();
          }

        /// <summary>
        /// A preview of all the included urls will be shown in the admin tool
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
          public void Preview_OnClick(object sender, EventArgs e)
          {
              if (ManifestFactory.Instance.LocalURLs.Values.Count == 0)
              {
                  InfoMessage.Text = "No File Generated";
                  InfoMessage.Visible = true;
                  InfoMessage.DataBind();
              }
              else
              {

                  foreach (var url in ManifestFactory.Instance.LocalURLs.Values)
                  {
                      FileListPanel.Controls.Add(new Literal() { Text = url });
                      FileListPanel.Controls.Add(new LiteralControl("<br />"));
                      FileListPanel.DataBind();
                  }

                  InfoMessage.Text = "Urls Being Shown in File";
                  InfoMessage.Visible = true;
                  InfoMessage.DataBind();
              }
          }
    }
}